package connection;

import shopping.Users;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

public class UsersDAO {
		// 获取所有用户的信息
		public ArrayList<Users> getAllUsers() {
			Connection conn = null;
			PreparedStatement ps = null;
			ResultSet rs = null;
			ArrayList<Users> list = new ArrayList<Users>(); // 所有商品的集合
			try {
				conn = DBConnection.getConnection();
				String sql = "select * from users;";
				ps = conn.prepareStatement(sql);
				rs = ps.executeQuery();
				while (rs.next()) {
					Users user = new Users();
					user.setNum(rs.getInt("num"));
					user.setName(rs.getString("name"));
					user.setPassword(rs.getString("password"));
					list.add(user);// 把商品加入到集合中
				}
				return list; // 返回集合
			} catch (Exception ex) {
				ex.printStackTrace();
				return null;
			} finally {

				// 关闭数据集对象
				if (rs != null) {
					try {
						rs.close();
						rs = null;
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}

				// 关闭PreparedStatement对象
				if (ps != null) {
					try {
						ps.close();
						ps = null;
					} catch (Exception ex) {
						ex.printStackTrace();
					}
				}
			}

		}
	// 获取指定用户名的用户信息
	public Users getUser(String name) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBConnection.getConnection();
			String sql = "select * from users where name=?;";
			ps = conn.prepareStatement(sql);
			ps.setString(1,name);
			rs = ps.executeQuery();
			if(rs.next()) {
				Users user = new Users();
				user.setNum(rs.getInt("num"));
				user.setName(rs.getString("name"));
				user.setPassword(rs.getString("password"));
				return user;
			}
			else return null;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		} finally {

			// 关闭数据集对象
			if (rs != null) {
				try {
					rs.close();
					rs = null;
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}

			// 关闭PreparedStatement对象
			if (ps != null) {
				try {
					ps.close();
					ps = null;
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}

	}

	// 注册新用户
	public int setNewuser(String name,String password) {
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			conn = DBConnection.getConnection();
			String sql = "insert into users(name,password) values(?,?);"; // SQL语句
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, password);
			return ps.executeUpdate();	
		} catch (Exception ex) {
			ex.printStackTrace();
			return 0;
		} finally {

			// 关闭数据集对象
			if (rs != null) {
				try {
					rs.close();
					rs = null;
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}

			// 关闭PreparedStatement对象
			if (ps != null) {
				try {
					ps.close();
					ps = null;
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}

		}
	}
}
